package com.bj.jiufu.bsns.sys.controller;

import com.bj.jiufu.base.AbstractBaseController;
import com.bj.jiufu.base.RespMsg;
import com.bj.jiufu.base.RespParam;
import com.bj.jiufu.bsns.sys.entity.User;
import com.bj.jiufu.bsns.sys.service.SysService;
import com.bj.jiufu.util.crypto.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by susen-pc on 2016/8/30.
 */
@Controller
@RequestMapping("/app")
public class SysController extends AbstractBaseController {

    @Autowired
    private SysService sysService;

    /**
     * 注册
     *
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "register", method = {RequestMethod.GET})
    public RespMsg register(HttpServletRequest req) {
        User user = this.getBody(req, User.class);
        user.setPassword(MD5.getMD5(user.getPassword()));
        RespParam respParam = sysService.regiester(user);
        return retRespMsg(req, respParam);
    }

    /**
     * 获取当前的app版本
     *
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getCurrAppVersion", method = {RequestMethod.GET})
    public RespMsg getCurrAppVersion(HttpServletRequest req) {
        RespParam respParam = sysService.getCurrAppVersion();
        return retRespMsg(req, respParam);
    }


    /**
     * 获取验证码
     *
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getCaptcha", method = {RequestMethod.GET})
    public RespMsg getCaptcha(HttpServletRequest req) {
        User user = this.getBody(req, User.class);
        RespParam respParam = sysService.getCaptcha(user.getMobile());
        return retRespMsg(req, respParam);
    }

    /**
     * 发送验证码
     *
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "sendCaptcha", method = {RequestMethod.GET})
    public RespMsg sendCaptcha(HttpServletRequest req) {
        User user = this.getBody(req, User.class);
        RespParam respParam = sysService.sendCaptcha(user.getMobile());
        return retRespMsg(req, respParam);
    }

    /**
     * 设置新秘密
     *
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "setNewPassword", method = {RequestMethod.GET})
    public RespMsg setNewPassword(HttpServletRequest req) {
        User user = this.getBody(req, User.class);
        RespParam respParam = sysService.setNewPassword(user.getMobile(), MD5.getMD5(user.getPassword()));
        return retRespMsg(req, respParam);
    }

    /**
     * 设置新秘密
     *
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "login", method = {RequestMethod.GET})
    public RespMsg login(HttpServletRequest req) {
        User user = this.getBody(req, User.class);
        RespParam respParam = sysService.login(user.getMobile(), MD5.getMD5(user.getPassword()));
        return retRespMsg(req, respParam);
    }

}
